/*
 * Copyright (c) 2024 Shenzhen Kaihong Digital Industry Development Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <stdint.h>
#include "sle_lite_advance_setting.h"
#include "sle_hilog.h"
#include "sle_connect_hal.h"

static double g_cover_mtu= 0.9;
static uint32_t g_max_mtu= 512;
static uint32_t g_max_data_size= 460;
static SleRecommendSendSize g_packageLenMap[SLE_MAX_CONNECT_ID_NUM];

void InitSleRecommendSendSize(void)
{
    for (int i = 0; i < SLE_MAX_CONNECT_ID_NUM; i++) {
        g_packageLenMap[i].connectId = 0xF;
        g_packageLenMap[i].recomSendDataSize = 0;
    }
}

void AddRecomSendDataToMap(uint16_t connectId, uint32_t dataSize)
{
    for(int i = 0; i < SLE_MAX_CONNECT_ID_NUM; i++) {
        if(g_packageLenMap[i].connectId == 0xF) {
            g_packageLenMap[i].connectId = connectId;
            g_packageLenMap[i].recomSendDataSize = dataSize;
            return;
        }
    }
    SLE_LOGE("AddRecomSendDataToMap failed");
}

void RemoveRecomSendDataToMap(uint16_t connectId)
{
    for(int i = 0; i < SLE_MAX_CONNECT_ID_NUM; i++) {
        if(g_packageLenMap[i].connectId == connectId) {
            g_packageLenMap[i].connectId = 0xF;
            g_packageLenMap[i].recomSendDataSize = 0;
            return;
        }
    }
    SLE_LOGW("no need RemoveRecomSendDataToMap");
}

SleErrorCode GetRecomSendDataSizeAdvance(uint16_t connectId, uint32_t *recomSendDataSize)
{
    bool isFind = false;
    for (int i = 0; i < SLE_MAX_CONNECT_ID_NUM; ++i) {
        if (connectId == g_packageLenMap[i].connectId) {
            *recomSendDataSize = g_packageLenMap[i].recomSendDataSize;
            isFind = true;
        }
    }
    if (!isFind) {
        SLE_LOGE("the device connectId[%{public}d] not find recomSendDataSize", connectId);
        *recomSendDataSize = 0;
    }
    return SLE_NO_ERROR;
}

uint32_t ConvertMtuToDataSize(uint32_t mtuSize)
{
    uint32_t dataSize = 0;
    if (mtuSize == g_max_mtu) {
        dataSize = g_max_data_size;
    } else {
        dataSize = (uint32_t)(mtuSize * g_cover_mtu);
    }
    return dataSize;
}

SleErrorCode SetRecomSendDataSizeAdvance(uint16_t connectId, uint32_t mtu)
{
    uint32_t dataSize = ConvertMtuToDataSize(mtu);
    bool isFind = false;
    for (int i = 0; i < SLE_MAX_CONNECT_ID_NUM; ++i) {
        if (connectId == g_packageLenMap[i].connectId) {
            g_packageLenMap[i].recomSendDataSize = dataSize;
            isFind = true;
        }
    }
    if (!isFind) {
        SLE_LOGW("AddRecomSendDataToMap connectId[%{public}d] - dataSize[%{public}d]", connectId, dataSize);
        AddRecomSendDataToMap(connectId, dataSize);
    }
    return SLE_NO_ERROR;
}
